package com.huilian.iotbox.data.utils;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.xml.bind.DatatypeConverter;
import java.security.spec.KeySpec;
import java.util.Arrays;

/**
 * 维码器街机DES加解密
 */
public class WeiMaQiDESUtil {


    /**
     * 备用秘钥4236443034383138
     */
    public static String keyHex = "4236453034393138";

    public static void main(String[] args) {
        try {
            /**
             * d950c07330fda914
             * 07B9CB1161C36BCB47D8BA1D53863314
             */
//            String dataHex = "";
//            String encryptedDataHex = DESEncrypt(dataHex, keyHex);
//            System.out.println("encryptedDataHex: " + encryptedDataHex);

            System.out.println("==========================");

            String encryptedDataHex = "A82161B4977EBA41";
            String decryptedDataHex = DESDecrypt(encryptedDataHex.replaceAll(" ", ""), keyHex);
            System.out.println("decryptedDataHex: " + decryptedDataHex);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加密
     *
     * @param dataHex
     * @param keyHex
     * @return
     */
    public static String DESEncrypt(String dataHex, String keyHex) {
        try {

            byte[] key = DatatypeConverter.parseHexBinary(keyHex);
            byte[] data = DatatypeConverter.parseHexBinary(dataHex);

            // 使用相同的密钥作为IV
            byte[] iv = Arrays.copyOf(key, 4);

            // 创建DES密钥规范
            KeySpec keySpec = new DESKeySpec(key);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(keySpec);

            // 创建初始化向量（IV）参数规范
            IvParameterSpec ivSpec = new IvParameterSpec(iv);

            // 创建DES加密器
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);

            // 执行加密
            byte[] encryptedData = cipher.doFinal(data);

            return DatatypeConverter.printHexBinary(encryptedData);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 解密
     *
     * @param encryptedDataHex
     * @param keyHex
     * @return
     */
    public static String DESDecrypt(String encryptedDataHex, String keyHex) {
        try {
            byte[] key = DatatypeConverter.parseHexBinary(keyHex);
            byte[] encryptedData = DatatypeConverter.parseHexBinary(encryptedDataHex);

            // 使用相同的密钥作为IV
            byte[] iv = Arrays.copyOf(key, 8);

            // 创建DES密钥规范
            KeySpec keySpec = new DESKeySpec(key);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(keySpec);

            // 创建初始化向量（IV）参数规范
            IvParameterSpec ivSpec = new IvParameterSpec(iv);

            // 创建DES解密器
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);

            // 执行解密
            byte[] decryptedData = cipher.doFinal(encryptedData);

            return DatatypeConverter.printHexBinary(decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
